<template>
  <div></div>
</template>
<script>
import { mapActions } from 'vuex'
import { timeFix } from '@/utils/util'
export default {
  data() {
    return {
      yamiToken: this.$route.query.yamitoken,
      hteToken: this.$route.query.hteToken,
      hipToken: this.$route.query.hipToken,
      srmToken: this.$route.query.srmToken,
      hipSecretKey: this.$route.query.hipSecretKey,
      backUrl: this.$route.query.backUrl,
      detailId: this.$route.query.detailId,
      ntpmToken: this.$route.query.ntpmToken,
      sso: this.$route.query.sso,
      reload: this.$route.query.reload,
    }
  },
  computed: {
    // backUrl(){
    //   if(this.$route.fullPath.indexOf("backUrl=")>-1){
    //     let backUrl = this.$route.fullPath.split("backUrl=")[1]
    //     backUrl = decodeURIComponent(backUrl)
    //     backUrl =  backUrl.split("&processInstanceId=")[0]
    //     backUrl =  backUrl.split("&devtype=")[0]
    //   //  backUrl = backUrl.replace(new RegExp("%2F","gm"),"/");
    //   //   backUrl = backUrl.replace(new RegExp("%3F","gm"),"?");
    //   //   backUrl = backUrl.replace(new RegExp("%3D","gm"),"=");
    //   //   console.info(backUrl)
    //     return backUrl
    //   }
    //   return "";
    // }
  },
  mounted() {
    this.$nextTick(() => {
      console.log('this.yamiToken', this.yamiToken)
      console.log('this.hteToken', this.hteToken)
      console.log('this.backUrl', this.backUrl)
      console.log('this.hipToken', this.hipToken)
      console.log('this.hipSecretKey', this.hipSecretKey)
      if (this.yamiToken !== '' && this.yamiToken !== undefined) {
        this.loginByToken({
          username: '',
          password: '',
          yamitoken: this.yamiToken,
        })
      } else if (this.hteToken !== '' && this.hteToken !== undefined) {
        this.loginByToken({
          username: '',
          password: '',
          hteToken: this.hteToken,
        })
      }else if (this.srmToken !== '' && this.srmToken !== undefined) {
        this.loginByToken({
          username: '',
          password: '',
          srmToken: this.srmToken,
        })
      } else if (
        this.hipToken !== '' &&
        this.hipToken !== undefined &&
        this.hipSecretKey !== '' &&
        this.hipSecretKey !== undefined
      ) {
        this.loginByToken({
          username: '',
          password: '',
          hipToken: this.hipToken,
          hipSecretKey: this.hipSecretKey.split(','),
        })
      } else if (this.ntpmToken !== '' && this.ntpmToken !== undefined) {
        this.loginByToken({
          username: '',
          password: '',
          ntpmToken: this.ntpmToken,
        })
      } else if (this.sso !== '' && this.sso !== undefined) {
        var ssoJson = JSON.parse(this.sso)
        this.loginByToken({
          username: '',
          password: '',
          sso: ssoJson,
        })
      } else if (this.reload == '1') {
          //重新加载页面
          this.$router.push({ path: this.backUrl })
      } else {
        this.$notification['error']({
          message: '非法登录',
          description: '',
          duration: 4,
        })
      }
    })
  },
  methods: {
    ...mapActions(['Login', 'Logout']),
    loginByToken(params) {
      let obj = params
      this.Login(obj)
        .then((res) => {
          console.log(res)
          if (!res.success) {
            this.$notification['error']({
              message: '登录失败',
              description: res.message,
              duration: 4,
            })
            return
          }
          debugger
          //登录成功后，如果当月还没有确认保密确认书，则跳转保密确认书页面
          if (res.result.confidentialityNo && res.result.confidentialityNo !== '') {
            let confidentialityUrl =
              '/confidentialityConfirm?backUrl=' +
              (this.backUrl ? this.backUrl : '') +
              '&detailId=' +
              (this.detailId ? this.detailId : '') +
              '&confidentialityNo=' +
              res.result.confidentialityNo
            this.$router.push({ path: confidentialityUrl }).catch(() => {
              console.log('登录跳转首页出错,这个错误从哪里来的')
            })
          } else {
            if (this.backUrl !== '' && this.backUrl !== undefined) {
              // URLEncoder.encode(backUrl,"utf-8");
              // // http://127.0.0.1:3000/hte-vue/blank?yamitoken=$TOKEN$&backUrl=/dailyTaskDetail?id=449334ca5d9c46aa93cbe224de741253&detailId=1436
              // if (this.backUrl.indexOf('dailyTaskDetail') != -1 || this.backUrl.indexOf('projectTaskDetail') != -1) {
              //   this.backUrl = this.backUrl + '&detailId='+this.detailId
              // }
              this.$router.push({ path: this.backUrl }).catch(() => {
                console.log('登录跳转首页出错,这个错误从哪里来的')
              })
            } else {
              // window.open('/homepage', "_blank");
              this.$router.push({ path: '/homepage' }).catch(() => {
                console.log('登录跳转首页出错,这个错误从哪里来的')
              })
              // this.$notification.success({
              //   message: '欢迎',
              //   description: `${timeFix()}，欢迎回来`,
              // });
            }
          }
        })
        .catch((err) => {
          this.$notification['error']({
            message: '登录失败',
            description: err.message,
            duration: 4,
          })
          window.console.log('login出现错误:' + err)
        })
    },
    getBackUrl() {
      if (this.$route.fullPath.indexOf('backUrl=') > -1) {
        let backUrl = this.$route.fullPath.split('backUrl=')[1]
        backUrl = backUrl.replace(new RegExp('%2F', 'gm'), '/')
        backUrl = backUrl.replace(new RegExp('%3F', 'gm'), '?')
        backUrl = backUrl.replace(new RegExp('%3D', 'gm'), '=')
        return backUrl
      }
      return null
    },
  },
}
</script>